import http from './http'

import VueI18n from '@/i18n'
// import VueI18n, { messages } from '@/i18n'
// import Vue from 'vue'

export const request = {}
// const messagesConst = messages[VueI18n.locale]
const language = VueI18n.locale
let key = 'Cn'
// 判断中英文
if (language === 'zh-CN') {
  key = 'Cn'
} else {
  key = 'En'
}
// const baseUrl = window.SITE_CONFIG['apiURL']
request.post = function (url, param, type) {
  // this.url = '/admin' + url
  this.url = url
  if (type && type.length > 0) {
    this.url = type + url
  }
  // this.param['language'] = key
  this.core = http.post(this.url, this.param)
  return this
}

request.postParam = function (url, param, type) {
  // this.url = '/admin' + url
  this.url = url
  if (type && type.length > 0) {
    this.url = type + url
  }
  this.core = http.post(this.url, param)
  return this
}
request.get = function (url, param, type) {
  // this.url = '/admin' + url
  this.url = url
  if (type && type.length > 0) {
    this.url = type + url
  }
  let params = {}
  var canSubmit = true
  if (param) {
    Object.keys(param).forEach(function (key) {
      if (param[key] instanceof Array) {
        if (param[key].length > 0) {
          param[key] = param[key].join(',')
        }
      }
    })
    params.params = param
    for (let item in params.params) {
      if (params.params[item] instanceof Array) {
        delete params.params[item]
      } else {
        if (isString(params.params[item])) {
          params.params[item] = params.params[item].trim()
        }
        if (item.indexOf(key) < 0) {
          param[item + key] = param[item]
        }
      }
    }
  } else {
    params.params = {}
  }
  if (canSubmit) {
    params.params['language'] = key
    this.core = http.get(this.url, params)
    return this
  } else {
  }
}
request.put = function (url, param, type) {
  this.url = url
  if (type && type.length > 0) {
    this.url = type + url
  }
  if (param) {
    this.param = param
    for (let item in param) {
      if (item.indexOf(key) > -1) {
        break
      }
      param[item + key] = param[item]
    }
  } else {
    this.param = {}
  }
  this.param['language'] = key
  this.core = http.put(this.url, this.param)
  return this
}

request.delete = function (url, param, type) {
  this.url = url
  if (type && type.length > 0) {
    this.url = type + url
  }
  let params = {}
  if (param) {
    params.params = param
    for (let item in params.params) {
      if (params.params[item] instanceof Array) {
        delete params.params[item]
      } else {
        if (isString(params.params[item])) {
          params.params[item] = params.params[item].trim()
        }
        if (item.indexOf(key) < 0) {
          param[item + key] = param[item]
        }
      }
    }
  } else {
    params.params = {}
  }
  params.params['language'] = key
  this.core = http.delete(this.url, params)
  return this
}
request.then = function (func) {
  this.core.then(data => {
    var newData = data.data
    newData.total = 0
    if (data.data && data.data.rows) {
      let records = data.data.rows
      newData.total = data.data.total
      newData['result'] = records
    } else {
      newData['result'] = newData.data
      newData.total = newData.total
    }
    func(newData['result'], newData.total, newData.code, newData)
  })
  return this
}
request.catch = function (func) {
  this.core.catch(data => {
    func(data)
  })
  return this
}
request.finally = function (func) {
  this.core.finally(func)
}

function isString (obj) {
  return obj === obj + ''
}

export default request
